package com.xmtlxx.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.entity.SysNode;
import com.xmtlxx.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xmtlxx.system.domain.bo.SysNodeBo;
import com.xmtlxx.system.domain.vo.SysNodeVo;
import com.xmtlxx.system.mapper.SysNodeMapper;
import com.xmtlxx.system.service.ISysNodeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 工作流节点Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-04-24
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysNodeServiceImpl implements ISysNodeService {

    private final SysNodeMapper baseMapper;

    /**
     * 查询工作流节点
     *
     * @param nid 工作流节点主键
     * @return 工作流节点
     */
    @Override
    public SysNodeVo queryById(String nid){
        return baseMapper.selectVoById(nid);
    }


    /**
     * 查询工作流节点列表
     *
     * @param bo 工作流节点
     * @return 工作流节点
     */
    @Override
    public List<SysNodeVo> queryList(SysNodeBo bo) {
        LambdaQueryWrapper<SysNode> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysNode> buildQueryWrapper(SysNodeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysNode> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWid()), SysNode::getWid, bo.getWid());
        lqw.eq(StringUtils.isNotBlank(bo.getPid()), SysNode::getPid, bo.getPid());
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysNode::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getOtype()), SysNode::getOtype, bo.getOtype());
        lqw.eq(StringUtils.isNotBlank(bo.getZflag()), SysNode::getZflag, bo.getZflag());
        lqw.eq(StringUtils.isNotBlank(bo.getTtype()), SysNode::getTtype, bo.getTtype());
        lqw.eq(StringUtils.isNotBlank(bo.getSflag()), SysNode::getSflag, bo.getSflag());
        lqw.eq(StringUtils.isNotBlank(bo.getEflag()), SysNode::getEflag, bo.getEflag());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysNode::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getNodeno()), SysNode::getNodeno, bo.getNodeno());
        lqw.eq(StringUtils.isNotBlank(bo.getNids()), SysNode::getNids, bo.getNids());
        lqw.gt(bo.getSort() != null, SysNode::getSort, bo.getSort());
        lqw.orderByAsc(SysNode::getSort);
        return lqw;
    }

    /**
     * 新增工作流节点
     *
     * @param bo 工作流节点
     * @return 结果
     */
    @Override
    public Boolean insertByBo(SysNodeBo bo) {
        SysNode add = BeanUtil.toBean(bo, SysNode.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setNid(IdUtil.simpleUUID());

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setNid(add.getNid());
        }
        return flag;
    }

    /**
     * 修改工作流节点
     *
     * @param bo 工作流节点
     * @return 结果
     */
    @Override
    public Boolean updateByBo(SysNodeBo bo) {
        SysNode update = BeanUtil.toBean(bo, SysNode.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysNode entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工作流节点
     *
     * @param ids 需要删除的工作流节点主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
